﻿using System;
using Icinetic.FeedCategorySample.Entities;
using Windows.Graphics.Display;
using Windows.UI.Xaml.Data;

namespace Icinetic.FeedCategorySample.Common.Converters
{
    public class DefaultImageConverter : IValueConverter
    {
        public DefaultImageConverter() { }

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            RssItem item = value as RssItem;
            try
            {
                if (string.IsNullOrWhiteSpace(item.Image))
                {
                    switch (DisplayProperties.CurrentOrientation)
                    {
                        case DisplayOrientations.Portrait:
                        case DisplayOrientations.PortraitFlipped:
                            return item.RowPortraitSpan > item.ColPortraitSpan ? "/Assets/noimage-ver.png" : "/Assets/noimage-hor.png";
                        case DisplayOrientations.Landscape:
                        case DisplayOrientations.LandscapeFlipped:
                        case DisplayOrientations.None:
                        default:
                            return item.RowSpan > item.ColSpan ? "/Assets/noimage-ver.png" : "/Assets/noimage-hor.png";
                    }
                }
                else
                    return item.Image;
            }
            catch 
            {
                return item.RowSpan > item.ColSpan ? "/Assets/noimage-ver.png" : "/Assets/noimage-hor.png";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class DefaultImageConverterPortada : IValueConverter
    {
        public DefaultImageConverterPortada() { }

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            RssItem item = value as RssItem;
            if (string.IsNullOrWhiteSpace(item.Image))
            {
                switch (DisplayProperties.CurrentOrientation)
                {
                    case DisplayOrientations.Portrait:
                    case DisplayOrientations.PortraitFlipped:
                        return "/Assets/noimage-ver.png";
                    case DisplayOrientations.Landscape:
                    case DisplayOrientations.LandscapeFlipped:
                    case DisplayOrientations.None:
                    default:
                        return "/Assets/noimage-hor.png";
                }
            }
            else
                return item.Image;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
